$(document).ready(function() {
	
});
function onLogin() {
	var username = $("#username").val();
	if (username == null || username == '') {
		showErrMsg("用户名或邮箱不能为空！");
		return false;
	}
	var password = $("#password").val();
	if (password == null || password == '') {
		showErrMsg("密码不能为空！");
		return false;
	}
	var obj = {
		"username": username,
		"password": password
	}
	var loading = layer.msg('登录中...', {
		icon: 16
		, shade: 0.3
		, time: false
	});
	$.ajax({
		type: "POST",
		url: "/login",
		data: JSON.stringify(obj),
		dataType: "json",
		headers: { "Content-Type": "application/json;charset=utf-8" },
		success: function(data) {
			layer.close(loading);
			if (data) {
				if (data.httpCode == 200) {
					window.location = '/index';
				} else {
					showErrMsg(data.msg);
				}
			}
		}, error: function(request) {
			layer.close(loading);
			showErrMsg("登录失败！");
		}
	});
}
function inputChange() {
	$("#errorHint").hide();
}

function showErrMsg(msg) {
	$("#errorHint").text(msg);
	$("#errorHint").show();
}

function openSynchBox() {
	$("#syBlack").show();
	$("#syBox").show();
}

function closeSyBox() {
	$("#syBlack").hide();
	$("#syBox").hide();
}

function onSynch() {
	var username = $("#syUsername").val();
	if (username == null || username == '') {
		layer.msg("请输入用户名或邮箱！", { icon: 2, time: 2000 });
		return false;
	}
	var password = $("#syPassword").val();
	if (password == null || password == '') {
		layer.msg("请输入密码！", { icon: 2, time: 2000 });
		return false;
	}
	var obj = { "username": username, "password": password };
	var loading = layer.msg('同步数据中...', {
		icon: 16
		, shade: 0.3
		, time: false
	});
	$.ajax({
		type: "POST",
		url: "/api/synch",
		data: JSON.stringify(obj),
		dataType: "json",
		headers: { "Content-Type": "application/json;charset=utf-8" },
		success: function(data) {
			layer.close(loading);
			if (data) {
				if (data.httpCode == 200) {
					layer.msg("同步数据完成！", { icon: 1, time: 2000 });
					closeSyBox();
				} else {
					layer.msg(data.msg, { icon: 2, time: 2000 });
				}
			}
		}, error: function(request) {
			layer.close(loading);
			layer.msg("数据同步失败，请重试！", { icon: 2, time: 2000 });
		}
	});
}
